package com.netistate.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.netistate.base.BaseDao;
import com.netistate.pojo.ProjectServiceDetail;
import com.netistate.util.ServiceAndInfoTypeMapUtil;

@Repository
public class ProjectServiceDetailDao extends BaseDao<ProjectServiceDetail, Integer>{

	/**
	 * 根据Pid查询所有的服务记录
	 * @param projectId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ProjectServiceDetail> findProjectServiceDetailByProjectId(
			int projectId) {
		
		String hql = "from ProjectServiceDetail where projectServices in (from ProjectServices where project ='" + projectId + "')";
		
		Query query = getSession().createQuery(hql);
		
		return query.list();
	}

	/**
	 * 根据pid和ProjectServiceType查询所有的服务记录
	 * @param pid
	 * @param psType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ProjectServiceDetail> findProjectServiceByServiceType(
			String pid, String psType) {
		int id = Integer.parseInt(pid);
		String hql = "";
		if("所有".equals(psType)){
			hql = "from ProjectServiceDetail where projectServicesid in (from ProjectServices where projectid ='" + id + "')";
		}else{
			String serviceNo = ServiceAndInfoTypeMapUtil.getServiceNo(psType);
			hql = "from ProjectServiceDetail where projectServicesid in (from ProjectServices where projectid ='" + id + "' and serviceNo = '" + serviceNo + "')";
		}
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	/**
	 * 根据信息类型查
	 * @param pid
	 * @param infoType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ProjectServiceDetail> findProjectServiceDetailByInfoType(
			String pid, String infoType) {
		int id = Integer.parseInt(pid);
		String hql = "";
		if("所有".equals(infoType)){
			hql = "from ProjectServiceDetail where projectServicesid in (from ProjectServices where projectid ='" + id + "')";
		}else{
			String infoNo = ServiceAndInfoTypeMapUtil.getInfoNo(infoType);
			hql = "from ProjectServiceDetail where projectServicesid in (from ProjectServices where projectid ='" + id + "') and infoTypeNo = '" + infoNo + "'" ;
		}
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	/**
	 * 根据projectServiceId去找对应的projectServiceDetail列
	 * @param psid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ProjectServiceDetail> findProjectServiceDetailByPSId(
			int psid) {
		String hql = "from ProjectServiceDetail where projectServicesid = ?";
		
		Query query = getSession().createQuery(hql);
		query.setParameter(0, psid);
		return query.list();
	}

	/**
	 * 根据psid去删除服务记录表定义的内容
	 * @param pssid
	 */
	public void delPSDByPSId(int pssid) {
		String hql = "delete ProjectServiceDetail where projectServicesid = ?";
		
		Query query = getSession().createQuery(hql);
		query.setParameter(0, pssid);
		query.executeUpdate();
	}

}
